package com.nblog.controller.admin;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.nblog.annotation.ControllerBind;
import com.nblog.auth.annotation.RequiresPermissions;
import com.nblog.constant.ApiConstant;
import com.nblog.interceptor.admin.LoginInterceptor;
import com.nblog.interceptor.admin.SysLogInterceptor;
import com.nblog.model.SysLog;
import com.nblog.plugin.ioc.annotation.JFAutowired;
import com.nblog.resp.ApiResp;
import com.nblog.service.SysLogService;
import com.nblog.utils.CommonUtils;

@ControllerBind(controllerKey="/admin/log")
@Before({LoginInterceptor.class,SysLogInterceptor.class})
public class LogController extends BaseControlller{
	@JFAutowired
	SysLogService sysLogService;
	@RequiresPermissions(value={"/admin/log"})
	public void index(){
		render("index.html");
	}
	@RequiresPermissions(value={"/admin/log"})
	public void getData(){
		String startTime=getPara("startTime");
		String endTime=getPara("endTime");
		StringBuffer sql=new StringBuffer("from sys_log Log,sys_user User where Log.uid=User.id ");
		if(StrKit.notNull(startTime,endTime)){
			sql.append("and Log.start_time>'"+startTime+"' and Log.start_time<'"+endTime+"' ");
		}else if(StrKit.notNull(startTime)){
			sql.append("and Log.start_time>'"+startTime+"' ");
		}else if(StrKit.notNull(endTime)){
			sql.append("and Log.start_time>'"+endTime+"' ");
		}
		sql.append(this.getOrderbyStr());
		Page<SysLog> page=SysLog.dao.paginate(getParaToInt("page", 1), getParaToInt("rows",20), "select Log.*,User.name as username ",sql.toString());
		System.out.println(page.getTotalRow());
		renderJson(CommonUtils.pageToDataGrid(page));		
	}
	@RequiresPermissions(value={"/admin/log"})
	public void del(){
		Db.update("truncate table sys_log");
		ApiResp resp=new ApiResp();
		resp.setCode(ApiConstant.CODE_SUCCESS);
		resp.setMsg("成功清空");
		renderJson(resp);
	}
}
